<?php
class EmailQueue extends CActiveRecord{
	public static function model($className=__CLASS__){
		return parent::model($className);
	}
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{emailqueue}}';
	}
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email','email'),
			array('email','required'),
			array('email','authenticate', 'skipOnError'=>true),
		);
	}
/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
//			'user_profile'=>array(self::HAS_ONE,'Profiles','user_id'),
//			'favors'=>array(self::HAS_MANY,'favor_user','user_id'),
			//'user_point'=>array(),
		);
	}
	public function attributeLabels(){
		return array(
			'email'=>'邮箱'
		);
	}
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('user_id',$this->user_id);

		$criteria->compare('user_name',$this->user_name,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('subject',$this->subject);

		$criteria->compare('content',$this->content);

		$criteria->compare('add_time',$this->add_time,true);

		$criteria->compare('send_time',$this->send_time,true);

		$criteria->compare('flag',$this->flag);

		$criteria->compare('md5_message',$this->md5_message);

		$criteria->compare('type',$this->type);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));

	}
	public function authenticate($attribute,$params){
		//$this->_identity=new UserIdentity($this->username,$this->password);
		//if(!$this->_identity->authenticate())
		//dbg($attribute,true);
		$em = User::model()->findByAttributes(array('email'=>$this->email));
		if(!$em)
			$this->addError('email','邮箱 地址不存在！');
	}
	public function saveEmailQueue($attr,$type='default'){
		
		$model = new EmailQueue();
		switch ($type) {
			case "forget":
				$t = time();
				$s = $model->findByAttributes(array('email'=>$attr['email'],'flag'=>'sending','type'=>'forget'));
				if($s){
					setcookie("email",urlencode($attr['email']),$t+10*60,"/");
					if (!isset($_COOKIE['email'])) {
					   $_COOKIE['email'] = urlencode($attr['email']);
					}
					return true;
				}
				$model->email = $attr['email'];
				$model->user_name = $attr['user_name'];
				$model->user_id = $attr['user_id'];
				$model->type = 'forget';
				$model->subject = 'newsbag找回密码';
				$model->content = '请按如下说明修改您的密码<br>尊敬的大地网用户hwh4729：<br>您好！<br>您在大地网上申请了找回NewsBag的密码，请复制以下链接确认更改。<br> http://'.$_SERVER['HTTP_HOST'].'/account/forget/go/'.md5($t.$attr['email']).'<br>请您务必在10分钟内新设密码，否则该链接将失效。<br><br><br>NewsBag 团队.';
				$model->add_time = $t;
				$model->send_time = $t;
				$model->md5_message = md5($t.$attr['email']);
				if($model->save(false)){
					setcookie("email",urlencode($attr['email']),$t+10*60,"/");
					if (!isset($_COOKIE['email'])) {
					   $_COOKIE['email'] = urlencode($attr['email']);
					}
					return true;
				}else{
					return false;
				}
			break;
			case 'register':
				$md5Message = md5('恭喜您注册成功为newsbag的一名用户，现在你可以随意添加并关注你喜欢的新闻。');
				$active_url = NC::get_header().'/account/'.urlencode($attr['user_id']).'/active/'.urlencode($md5Message);
				$model->attributes = $attr;
				$model->email = $attr['email'];
				$model->user_name = $attr['user_name'];
				$model->user_id = $attr['user_id'];
				$model->subject = 'newsbag注册邮箱确认';
				$model->content = '恭喜您注册成功为newsbag的一名用户，我们需 要对您的地址有效性进行验证以避免垃圾邮件或地址被滥用，您只需点击下面的链接即可激活你的帐号：<br>'.$active_url.'<br>'.
				'(如果上面不是链接形式，请将地址手工粘贴到浏览器地址栏再访问)<br><br>感谢您的访问，祝您使用愉快！<br>此致<br>NewsBag 团队.';
				$model->add_time = time();
				$model->send_time = time();
				$model->md5_message = $md5Message;
				$model->save();
			break;
			default:
				
			break;
		}
		
	}
	public function getEmailList(){
		$result = self::model()->findAll(array(
			'condition'=>'flag = "'.sending.'"',
		));
		return $result;
	}
	public function updateEmailFlag($model,$str){
		$model->flag = $str;
		$model->save();
	}
}